from google.appengine.ext import db
from Common.User import User
from Common.Campaign import Campaign
from Common.Client import Client
from Common.CompanyOrg import Company
from Common.Reward import Reward

from datetime import date
from datetime import timedelta
import datetime

class HIT(db.Model):
    """
    This object is used to store submited HITs in our system 
    """
    Title = db.StringProperty()
    Description = db.StringProperty()
    
    LifetimeInSeconds = db.IntegerProperty()
    MaxAssignments = db.IntegerProperty()
    RequesterAnnotation = db.StringProperty() 
    AssignmentDurationInSeconds = db.IntegerProperty()
    Keywords = db.StringProperty()
    AutoApprovalDelayInSeconds = db.IntegerProperty()
    
    QuestionForm = db.TextProperty()
    
    HITId = db.StringProperty()
    
    Amount = db.FloatProperty(required=False)
    
    CreatedDate = db.DateTimeProperty()
    
    Ref_Requester = db.ReferenceProperty(User, collection_name='HITs')
    Ref_Campaign = db.ReferenceProperty(Campaign, collection_name='HITs')
    Ref_Client = db.ReferenceProperty(Client, collection_name='HITs')
    Ref_Company = db.ReferenceProperty(Company, collection_name='HITs')
    # We need a separated reward object
    #Amount = db.FloatProperty()
    #CurrencyCode = db.StringProperty()
    
    #  
    #HITTypeId = db.StringProperty()
    
    # it already has reference to question form
    #Question = db.StringProperty(required=True)
    
    
    # This is not necessary
    #HITPropertiesSetting=db.ReferenceProperty(HITPropertiesSetting)
    
    # since HIT use numeric id, the transaction put is  not required.
    # please user put method directly
    #def put (self):
    #    # Make sure e-mails are unique for each user
    #    if (not self.is_saved()) and HIT.get_by_key_name(self.Key().Id()):
    #        raise DuplicatedInstanceError ('HIT.Key().Id()', self.Key().Id())
    #    db.Model.put (self)
    
    def getExpirationText(self):
        expirationDate = self.CreatedDate + timedelta(seconds=self.LifetimeInSeconds)
        expiration = expirationDate - datetime.datetime.now()
        if(expiration.days < 0):
            return expirationDate.strftime("%B %d, %Y") + " (0 days 0 hours)"
    
        return expirationDate.strftime("%B %d, %Y") + " (" +self.getTimeText(expiration)+ ")"
    
    def getDurationText(self):
        t = timedelta(seconds=self.AssignmentDurationInSeconds)
        return self.getTimeText(t)
    
    def getTimeText(self, ctime):
        days = ctime.days
        seconds = ctime.seconds
        hours = seconds / 3600
        minutes = (seconds - hours * 3600) / 60
        
        ctext = ""
        if days != 0:
            ctext = str(days) + " days"
        if hours != 0:
            ctext = ctext + " " + str(hours) + " hours"
        if minutes != 0:
            if hours == 0 and days != 0:
                ctext = ctext + " 0 hours"
            ctext = ctext + " " + str(minutes) + " mins"
        return ctext
